package com.demo;

import java.io.File;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;

public class DemoTermRangeQuery {

	private static final String PATH = "d:\\zsl\\index16";

	public static void createIndexSort() throws Exception {
        //1、创建一个Director对象，指定索引库保存的位置。

        //1.1把索引库保存在内存中
        //Directory directory = new RAMDirectory();

        //1.2把索引库保存在磁盘
        Directory directory = FSDirectory.open(new File(PATH).toPath());

        //2、基于Directory对象创建一个IndexWriter对象
        IndexWriterConfig config = new IndexWriterConfig();
        IndexWriter indexWriter = new IndexWriter(directory, config);

        String str = "abcd abce abcf abcg abch abci abcj abck abcl abcm abcn abco abcp abcq abcr";
        String[] arr = str.split(" ");
        for (String s : arr) {
        	Document document = new Document();
        	document.add(new TextField("content", s, Field.Store.YES));
        	indexWriter.addDocument(document);
		}
        Document document = new Document();
        document.add(new TextField("content", "bcd", Field.Store.YES));
        document.add(new TextField("name", "Andy", Field.Store.YES));
        indexWriter.addDocument(document);
        document = new Document();
        document.add(new TextField("content", "ga", Field.Store.YES));
        document.add(new TextField("name", "Jack", Field.Store.YES));
        indexWriter.addDocument(document);
        document = new Document();
        document.add(new TextField("content", "gc", Field.Store.YES));
        document.add(new TextField("name", "Tom", Field.Store.YES));
        indexWriter.addDocument(document);
        document = new Document();
        document.add(new TextField("content", "gch", Field.Store.YES));
        document.add(new TextField("name", "Pony", Field.Store.YES));
        indexWriter.addDocument(document);
        document = new Document();
        document.add(new TextField("content", "gchb", Field.Store.YES));
        document.add(new TextField("name", "Jolin", Field.Store.YES));
        indexWriter.addDocument(document);

        //6、关闭indexwriter对象
        indexWriter.close();
    }

	public static void searchIndexSort() throws Exception {
        //1、创建一个Director对象，指定索引库的位置
        Directory directory = FSDirectory.open(new File(PATH).toPath());
        //2、创建一个IndexReader对象
        IndexReader indexReader = DirectoryReader.open(directory);
        //3、创建一个IndexSearcher对象，构造方法中的参数indexReader对象。
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        Query query = new TermRangeQuery("content", new BytesRef("bc"), new BytesRef("gch"), true, true);
        TopDocs topDocs = indexSearcher.search(query, 3);
        //6、取查询结果的总记录数
        System.out.println("查询总记录数：" + topDocs.totalHits);
        //7、取文档列表
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        //8、打印文档中的内容
        for (ScoreDoc doc : scoreDocs) {
            //取文档id
            int docId = doc.doc;
            //根据id取文档对象
            Document document = indexSearcher.doc(docId);
            System.out.println(docId);
            System.out.println(document.get("name"));
            System.out.println(document.get("content"));

        }
        //9、关闭IndexReader对象
        indexReader.close();
    }

	public static void main(String[] args) throws Exception {
		//createIndexSort();
		searchIndexSort();
	}

}
